package JDKtest.java_lang_test.List;
import JDKtest.java_lang_test.List.ListNode;

public class ListDone {

    public static ListNode reverseList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode pre = null;
        ListNode cur = head;
        while(cur != null){
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }

        return pre;

    }


    public static String NodeToString(ListNode head) {
        StringBuilder sb = new StringBuilder();
        ListNode current = head;

        while (current != null) {
            sb.append(current.val);
            if (current.next != null) {
                sb.append("");
            }
            current = current.next;
        }

        return sb.toString();
    }


    public static void main(String[] args) {
        ListNode node1= new ListNode(1,new ListNode(2,new ListNode(3)));
        ListNode result = reverseList(node1); // change the current nodelist
        // print the result
        String resultString = NodeToString(result);
        System.out.println(resultString);
        System.out.println();


    }
}